package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import com.tanhua.server.service.UsersService;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.MalformedInputException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserService userService;

    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询用户信息
     * */
    @GetMapping
    public ResponseEntity users(Long userID, @RequestHeader("Authorization") String token){
        Long userId = userID;
        //判断id
        if (userId == null){
//            //判断token
//            boolean verifyToken = JwtUtils.verifyToken(token);
//            if (!verifyToken){
//                throw new RuntimeException("用户未登入");
//            }
//            //解析token
//            Claims claims = JwtUtils.getClaims(token);
//            userId = claims.get("id", Long.class);
           userId = UserHolder.getUserId();
        }

        UserInfoVo userInfoVo =   usersService.findUserInfoById(userId);
        return ResponseEntity.ok(userInfoVo);
    }
    /**
     * 更新用户信息
     * */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
//       /// 判断token
//            boolean verifyToken = JwtUtils.verifyToken(token);
//            if (!verifyToken){
//                throw new RuntimeException("用户未登入");
//            }
//     //   解析token
//            Claims claims = JwtUtils.getClaims(token);
//        Long   userId = claims.get("id", Long.class);
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        usersService.updateUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询通用设置
     * */
    @GetMapping("/settings")
    public ResponseEntity settings(){
       SettingsVo vo = usersService.findSettings();

       return ResponseEntity.ok(vo);

    }

    /**
     * 修改陌生人问题
     * */

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){

        //获取参数
        String content = map.get("content").toString();
        //保存问题
        usersService.saveQuestions(content);
        return ResponseEntity.ok(null);

    }
    /**
     * 修改通知
     * */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map){
        //获取参数
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        //保存
        usersService.updateSetting(likeNotification,pinglunNotification,gonggaoNotification);

        return ResponseEntity.ok(null);
    }
    /**
     * 查询黑名单
     * */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult vo =  usersService.blacklist(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 移除黑名单
     * */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlack(@PathVariable("uid") Long id){

        usersService.deleteBlack(id);

        return ResponseEntity.ok(null);

    }
    /**
     * 修改手机号-发送验证码
     *
     * */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //获取手机号
        Long userId = UserHolder.getUserId();
       User user = userService.findById(userId);
        //发送验证码
        String code = "123456";
        //调用方法发送短信
        // smsTemplate.sendSms(phone,code);
        System.out.println("发送成功：" + code);
        //存入redis
        redisTemplate.opsForValue().set("CODE:"+user.getMobile(),code,5, TimeUnit.MINUTES);

        return ResponseEntity.ok(null);
    }
    /**
     * 校验验证码
     * */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //获取手机号
        Long userId = UserHolder.getUserId();
        User user = userService.findById(userId);
        //获取验证码
        String code = map.get("verificationCode").toString();
        //判断验证码
        Map map1 = userService.checkVerificationCode(user.getMobile(), code);
        return ResponseEntity.ok(map1);
    }
    /**
     * 保存手机号
     * */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        //获取新手机号
        String phone = map.get("phone").toString();
        usersService.updatePhone(phone);

        return ResponseEntity.ok(null);
    }
}
